package org.m8.auth.config;

import com.xingyuv.jushauth.config.AuthConfig;
import com.xingyuv.jushauth.config.AuthDefaultSource;
import com.xingyuv.justauth.autoconfigure.CacheProperties;
import com.xingyuv.justauth.autoconfigure.JustAuthProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * justauth
 *
 * @author yidanjun
 * @date 2024/9/6
 **/
@Configuration
public class JustAuthConfig {

    @Bean
    @Primary
    public JustAuthProperties justAuthProperties(SocialConfig socialConfig){
        JustAuthProperties justAuthProperties = new JustAuthProperties();
        justAuthProperties.setEnabled(true);
        CacheProperties cache = new CacheProperties();
        cache.setType(CacheProperties.CacheType.REDIS);
        cache.setTimeout(Duration.ofHours(24));
        justAuthProperties.setCache(cache);

        Map<String, AuthConfig> type = new HashMap<>();

        String clientId = socialConfig.getClientId() == null ? "" : socialConfig.getClientId();
        String clientSecret = socialConfig.getClientSecret() == null ? "" : socialConfig.getClientSecret();
        type.put(AuthDefaultSource.WECHAT_MINI_APP.name(), AuthConfig.builder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                        .redirectUri("http://127.0.0.1:8080")
                        .ignoreCheckState(true)
                .build());
        justAuthProperties.setType(type);

        return justAuthProperties;
    }


}
